classdef RigolDG4202OutPort < MeasurementComponent.MachineUnit.IOPort.OutputPort
    %TEKAFG3252OUTPORT
    properties
        index
    end
    
    methods
        function obj = RigolDG4202OutPort(device, index)
            %RIGOLDG4202OUTPORT 
            devCheck = @(x)validateattributes(x, {'EquipmentControl.SignalSource.RigolDG4202'}, {'scalar'});
            idxCheck = @(x)ismember(x, [1,2]);
            p = inputParser;
            p.addRequired('device', devCheck);
            p.addRequired('index', idxCheck);
            p.parse(device, index);
            
            name = sprintf('%s_Out%d', device.dev_name, index);
            obj@MeasurementComponent.MachineUnit.IOPort.OutputPort(device, name);
            obj.index = index;
        end
        
        function obj = enable(obj)
            obj.device.fire(obj.index);
        end
        function obj = disable(obj)
            obj.device.silence(obj.index);
        end
    end
    
    %%
    methods
        function obj = setup(obj, varargin)
            dev = obj.device;
            obj.last_setting = obj.getCurrentSetting();
            
            p=inputParser;
            p.addParameter('shape',     obj.last_setting.wave_shape,     @(x) ismember(upper(x), {'SIN', 'SQU', 'PUL', 'RAMP', 'PRN', 'USER', 'USER1', 'USER2', 'USER3', 'USER4'}));
            p.addParameter('frequency', obj.last_setting.wave_frequency,       @isnumeric);
            p.addParameter('phase',     obj.last_setting.wave_phase, @isnumeric);
            p.addParameter('amplitude', obj.last_setting.wave_amplitude,      @isnumeric);
            p.addParameter('offset',    obj.last_setting.wave_offset,   @isnumeric);
            p.addParameter('symm',      obj.last_setting.wave_symm,  @isnumeric);
            p.parse(varargin{:});
            
            dev.setWave(obj.index, p.Results);
            obj.current_setting = obj.getCurrentSetting();
        end
        function setting = getCurrentSetting(obj)
            obj.device.updateStatus();
            setting.wave_shape = obj.device.wave_shape{obj.index};
            setting.wave_frequency = obj.device.wave_frequency{obj.index};
            setting.wave_phase = obj.device.wave_phase{obj.index};
            setting.wave_amplitude = obj.device.wave_amplitude{obj.index};
            setting.wave_offset = obj.device.wave_offset{obj.index};
            setting.wave_symm = obj.device.wave_symm{obj.index};
            setting.output_impedance = obj.device.output_impedance{obj.index};
            setting.output_polarity = obj.device.output_polarity{obj.index};
            setting.output_state = obj.device.output_state{obj.index};
            obj.current_setting = setting;
        end
        function obj = recoverLastSetting(obj)
            obj.setup(obj.last_setting);
        end        
        function obj = initialize(obj)
            default_setting.shape = 'SIN';
            default_setting.frequency = 1e3;
            default_setting.phase = 0.0;
            default_setting.amplitude = 1.0;
            default_setting.offset = 0.0;
            default_setting.symm = 50;
            obj.setup(default_setting);
        end
    end
    %%
    methods
        function node = getScopeNode(obj)
            L = log4m.getLogger();
            node = [];
            L.info('RigolDG4202OutPort::getScopeNode', sprintf('%s does not support this methods.', class(obj)));
        end
        function val = getDC(obj)
            currSetting = obj.getCurrentSetting();
            val = currSetting.wave_offset;
        end
        function setDC(obj, value)
            obj.setAWG([1,1,1], 'offset', value);
        end
        function setAC(obj, varargin)
            currSetting = obj.getCurrentSetting();
            p=inputParser;
            p.addParameter('shape',     currSetting.wave_shape,     @(x) ismember(upper(x), {'SIN', 'SQU', 'PUL', 'RAMP', 'PRN', 'USER', 'USER1', 'USER2', 'USER3', 'USER4'}));
            p.addParameter('frequency', currSetting.wave_frequency,       @isnumeric);
            p.addParameter('phase',     currSetting.wave_phase, @isnumeric);
            p.addParameter('amplitude', currSetting.wave_amplitude,      @isnumeric);
            p.addParameter('offset',    currSetting.wave_offset,   @isnumeric);
            p.addParameter('symm',      currSetting.wave_symm,  @isnumeric);
            p.parse(varargin{:});
            
            obj.setup(p.Results);
            obj.enable();
        end
        function setAWG(obj, waveform, varargin)
            currSetting = obj.getCurrentSetting();
            p=inputParser;
            p.addParameter('frequency', currSetting.wave_frequency,       @isnumeric);
            p.addParameter('phase',     currSetting.wave_phase,     @isnumeric);
            p.addParameter('amplitude', currSetting.wave_amplitude,      @isnumeric);
            p.addParameter('offset',    currSetting.wave_offset,   @isnumeric);
            p.parse(varargin{:});
            obj.device.setAWGWaveform(obj.index, waveform); pause(1);
            obj.setup(p.Results);
            obj.enable();
        end
        
        function setImpedance(obj, imp)
            obj.device.setOutputImpedance(obj.index, imp);
        end
    end
end

